package thread.threadlocal;

import java.lang.ref.WeakReference;

/**
 * @Author: ycy
 * @CreateDate: 2019/10/11 14:15
 */
public class Sale extends WeakReference<Apple> {

  public Sale(Apple referent) {
    super(referent);
  }

  // 演示一下 WeakReference的用法
  public static void main(String[] args) {
//    Apple apple = new Apple();
//    System.out.println(apple);

    Sale sale = new Sale(new Apple());
    Apple apple1 = sale.get();
    System.out.println(apple1);

    //帮助gc
//    apple = null;

    //设置 jvm xmx10m 发生一次gc
    for (int i = 0; i < 15; i++) {
      byte[] a = new byte[1024 * 1000]; // 1M
    }
    System.gc();//gc 之后apple就被回收了

//    try {
//      Thread.sleep(2000);
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    }

    //如果为空，代表被回收了
    while (sale.get() != null) {
    }
    System.out.println("clear Apple。");

  }

}


